/**
 * @desc 价格公共组件
 * @props  {string or number} price 价格值
 * @props  {string} type    乘除类型 乘：multiply  除：divide
 * @props  {string} className     样式名称
 * @props  {string} symbol   价格前面的符号 默认为￥
 * @props  {string}    价格保留小数位数 默认为 0
 */
import React from 'react'
import PropTypes from 'prop-types'
import { transNumber } from "@/utils/numberTrans";

class TheMoney extends React.Component {
    formatting = (s) => {
        let intSum = s.substring(0, s.indexOf(".")).replace(/\B(?=(?:\d{3})+$)/g, ',');//取到整数部分
        let dot = s.substring(s.length, s.indexOf("."))//取到小数部分搜索
        return intSum + dot
    }

    render() {
        const { price, type, className, symbol, numFixed, del } = this.props;
        return (
            <div style={{ display: 'inline-block' }} className={className}>
                {
                    del ? <span><del style={{ color: '#999' }}>{symbol}</del></span> : <span>{symbol}</span>
                }

                {
                    del ? (type === 'divide' ? <span><del style={{ color: '#999' }}>{this.formatting(transNumber((Number.parseFloat(price) / 100), numFixed))}</del></span> : <span><del style={{ color: '#999' }}>{this.formatting(transNumber((Number.parseFloat(price) * 100), numFixed))}</del></span>) : ((type === 'divide' ? <span>{this.formatting(transNumber((Number.parseFloat(price) / 100), numFixed))}</span> : <span>{this.formatting(transNumber((Number.parseFloat(price) * 100), numFixed))}</span>))
                }

            </div>
        )
    }
}

TheMoney.propTypes = {
    price: PropTypes.number || PropTypes.string,
    type: PropTypes.string,
    numFixed: PropTypes.number,
    symbol: PropTypes.string,
    del: PropTypes.bool
}
TheMoney.defaultProps = {
    price: 0,
    type: 'divide',
    symbol: '￥',
    numFixed: 2,
    del: false
}
export default TheMoney
